package com.kdtech.analyse.news;
import com.kdtech.analyse.AnalyseNews;
import com.kdtech.utils.HtmlCleaner;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.kdtech.analyse.JSoupUtils;
import com.kdtech.crawler.CrawlHTML;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.NumberUtils;
import com.kdtech.utils.RegexUtils;
import com.kdtech.utils.StringUtils;
import com.kdtech.utils.HtmlCleaner;

/**
 * <新闻 - 龙虎网> 解析类
 *
 * @author KK
 *
 */
public class LonghooNewsAnalyse implements AnalyseNews {



	
	public boolean isDetailPage(String url) {
		String[] regex={
				"http://news.longhoo.net/[a-z]*/content/[0-9]{4}-[0-9]{2}/[0-9]{2}/content_[0-9]*.htm",
				"http://news.longhoo.net/[a-z]*/[a-z]*/content/[0-9]{4}-[0-9]{2}/[0-9]{2}/content_[0-9]*.htm",
				"http://[a-z]*.longhoo.net/[0-9]{4}-[0-9]{2}/[0-9]{2}/content_[0-9]*.htm",
				"http://[a-z]*.longhoo.net/html/[a-z]*/[0-9]{4}/[0-9]{4}/[0-9]*.html",
				"http://[a-z]*.longhoo.net/a/[a-z]*/[0-9]{8}/[0-9]*.html",
				"http://mynews.longhoo.net/thread-[0-9].*-1-1.html"
		};

		return RegexUtils.matchAny(url,regex);
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {

		if (urlMeta.getHtml() == null) {
		}
		String htmltxt=urlMeta.getHtml();
		String url=urlMeta.getUrl();

		String title=null;
		String content=null;
		String author=null;
		Long date=null;
		String dateStr=null;
		String commnetNum=null;
		String clickNum=null;
		Document doc=Jsoup.parse(htmltxt);

		title=doc.select("div.content_content  div.content_title ").text();
		dateStr=doc.select("div.content_content  div.bianji").text();
		date=DateUtils.matchDate(dateStr);
		content=HtmlCleaner.getContentHtml(url,doc.select("div.content_content  div#ct,div.list,div.list4"));


		if(StringUtils.isNullOrEmpty(title)){
			title=doc.select("div.lf_content div.content_title").text();
			dateStr=doc.select("div.lf_content div.list4 div.bianji").text();
			date=DateUtils.matchDate(dateStr);
			content=HtmlCleaner.getContentHtml(url,doc.select("div.lf_content div.list4 > p"));
		}

		if(StringUtils.isNullOrEmpty(title)){
			title=doc.select("div.article_box > div.article_left > div.article_left_box").text();
			dateStr=doc.select("div.article_box > div.article_left > div.article_left_box1").text();
			date=DateUtils.matchDate(dateStr);
			content=HtmlCleaner.getContentHtml(url,doc.select("div.article_box > div.article_left > div.article_left_box2"));
		}

		if(StringUtils.isNullOrEmpty(title)){
			title=doc.select("div.article h1.artical_topic").text();
			dateStr=doc.select("div.article div.artical_sth").text();
			date=DateUtils.matchDate(dateStr);
			content=HtmlCleaner.getContentHtml(url,doc.select("div.article div#artical_bk"));
		}
		if(StringUtils.isNullOrEmpty(title)){
			title=doc.select("div.content > div.rightart > div.artt1 > h4").text();
			dateStr=doc.select("div.content > div.rightart > div.artt1 > p").text();
			date=DateUtils.matchDate(dateStr);
			content=HtmlCleaner.getContentHtml(url,doc.select("div.content > div.rightart > div.words"));
		}
		if(StringUtils.isNullOrEmpty(title)){
			title=doc.select("div.all > div.main-lm > div.word-f > div.newxw > h1").text();
			dateStr=doc.select("div.all > div.main-lm > div.word-f > div.newxw > p.p-w").text();
			date=DateUtils.matchDate(dateStr);
			content=HtmlCleaner.getContentHtml(url,doc.select("div.all > div.main-lm > div.word-f > div.newxw > div.wordz"));
		}
		if(StringUtils.isNullOrEmpty(title)){
			title=doc.select("div#part1 > div.left > div.article > h1").text();
			dateStr=doc.select("div#part1 > div.left > div.article > div > div.date").text();
			date=DateUtils.matchDate(dateStr);
			content=HtmlCleaner.getContentHtml(url,doc.select("div#part1 > div.left > div.article > div.content"));
		}
		if(StringUtils.isNullOrEmpty(title)){
			title=doc.select("div.content > div.section > dl > dd > div.main > h1").text();
			dateStr=doc.select("div.content > div.section > dl > dd > div.main > div.titBar").text();
			date=DateUtils.matchDate(dateStr);
			content=HtmlCleaner.getContentHtml(url,doc.select("div.content > div.section > dl > dd > div.main > div.bd"));
		}
		if(StringUtils.isNullOrEmpty(title)){
			title=doc.select("div.content > div.content_2 > div.content_3 > div.list1 > div.content_title").text();
			dateStr=doc.select("div.content > div.content_2 > div.content_3 > div.list1 > div.bianji").text();
			date=DateUtils.matchDate(dateStr);
			content=HtmlCleaner.getContentHtml(url,doc.select("div.content > div.content_2 > div.content_3 > div.list1 > div > p"));
		}
		if(StringUtils.isNullOrEmpty(title)){
			title=doc.select("div.content > div.plate_lf > div.content_lf > div.content_lf_content > div.list4 > div.content_title > h3.h3_1").text();
			dateStr=doc.select("div.content > div.plate_lf > div.content_lf > div.content_lf_content > div.list4 > div.bianji").text();
			date=DateUtils.matchDate(dateStr);
			content=HtmlCleaner.getContentHtml(url,doc.select("div.content > div.plate_lf > div.content_lf > div.content_lf_content > div.list4 > p"));
		}
		if(StringUtils.isNullOrEmpty(title)){
			title=doc.select("div.xg_Top1 > div.xg_body > div.lm_xg > div.lm_right > div.xglb_lm > div.top_wzy > h3").text();
			dateStr=doc.select("div.xg_Top1 > div.xg_body > div.lm_xg > div.lm_right > div.xglb_lm > div.top_wzy ").text();
			date=DateUtils.matchDate(dateStr);
			content=HtmlCleaner.getContentHtml(url,doc.select("div.xg_Top1 > div.xg_body > div.lm_xg > div.lm_right > div.xglb_lm > div.nr"));
		}

		if(StringUtils.isNullOrEmpty(title)){
			title=doc.select("#thread_subject").text();
			date=JSoupUtils.matchDate(doc, "发表于");
			doc.select("ignore_js_op").remove();
			content=HtmlCleaner.getContentHtml(url,doc.select("td[id^=postmessage_]"));
		}

		if(StringUtils.isBlank(title)){
			title=doc.select("title").text();
			title=StringUtils.substringBefore(title, "-");
		}

		if (StringUtils.isBlank(dateStr)) {
			dateStr=doc.select("div.artical").text();
			if (StringUtils.isBlank(dateStr)) {
				dateStr=doc.select("div.bianji").text();
			}
		}

		if (StringUtils.isBlank(content)) {
			content=HtmlCleaner.getContentHtml(url,doc.select("div.articalReal"));
		}
		if (StringUtils.isBlank(content)) {
			content=HtmlCleaner.getContentHtml(url,doc.select("div.right_lm_nr"));
		}

		if(StringUtils.isBlank(content)){
			content=HtmlCleaner.getContentHtml(url,doc.select("td.plc td.t_f"));
			if(StringUtils.isBlank(content)){
				content=HtmlCleaner.getContentHtml(url,doc.select("div.content_body"));
			}
		}

		String authorStr=null;
		authorStr=doc.select("div.authi a.xi2").text();
		if(StringUtils.isBlank(authorStr)){
			authorStr=doc.select("div.bianji").text();
		}
		author=StringUtils.substringBetween(authorStr, "来源:", " ");

		if(date==null){
			date=DateUtils.matchDate(dateStr);
		}
		if(date==null){
			date=DateUtils.matchDate(doc.select("span[title]").text());
		}
		if (date == null) {
			date=DateUtils.matchDate(url);
		}
			NewsMeta news=new NewsMeta();
			news.setUrl(url);
			news.setType(1);
			news.setTitle(StringUtils.trimSpace(title));
			news.setCommentNum(NumberUtils.parseInt(commnetNum));
			news.setClickNum(NumberUtils.parseInt(clickNum));
			news.setContent(StringUtils.trimSpace(content));
			news.setDate(date);
			news.setAuthor(author);
			return news;
	}



	public static void main(String[] args) {
		LonghooNewsAnalyse a=new LonghooNewsAnalyse();
		String url="http://xuanwu.longhoo.net/html/xuanwuxinwen/2015/1124/3572.html";
		System.out.println(a.isDetailPage(url));
		UrlMeta meta=CrawlHTML.responseToURL(url);
		NewsMeta parserHtml=a.parserHtml(meta);
		System.out.println(parserHtml);
	}

	
	public NewsMeta Update(NewsMeta meta) {
		return null;
	}
	
	public boolean isNeedUpdate(){
		return false;
	}
}
